using System;
using System.Transactions;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.ConnectionPool;
using OracleInternal.ServiceObjects;

namespace OracleInternal.MTS
{
	internal class CCPMTSTxnRM : MTSTxnRM
	{
		internal CCPMTSTxnRM()
			: base(bIsCCP: true)
		{
		}

		~CCPMTSTxnRM()
		{
		}

		internal override void ReleaseRPs(SyncQueueList<ConnectionString> csList, Transaction txn)
		{
			ConnectionString connectionString = null;
			while ((connectionString = csList.Dequeue()) != null)
			{
				try
				{
					OracleConnectionDispenser<OraclePoolManager, OraclePool, OracleConnectionImpl>.RemoveRM(connectionString, m_serviceName, txn);
				}
				catch
				{
				}
			}
		}

		internal override void ReleaseRP(ConnectionString cs, Transaction txn)
		{
			if (cs != null)
			{
				try
				{
					OracleConnectionDispenser<OraclePoolManager, OraclePool, OracleConnectionImpl>.RemoveRM(cs, m_serviceName, m_sysTxn);
				}
				catch
				{
				}
			}
		}

		internal override void MTSTransactionCompleted(object sender, TransactionEventArgs e)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)4352, OracleTraceClassName.CCPMTSTxnRM, OracleTraceFuncName.MTSTransactionCompleted, ToString());
			}
			try
			{
				e.Transaction.TransactionCompleted -= MTSTransactionCompleted;
				if (string.Compare(m_txnLocalID, e.Transaction.TransactionInformation.LocalIdentifier, ignoreCase: true) != 0)
				{
					return;
				}
				if (m_enlistedState == EnlistedState.Distributed)
				{
					try
					{
						DetachBranches();
					}
					catch
					{
					}
				}
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.MTS, OracleTraceClassName.CCPMTSTxnRM, OracleTraceFuncName.MTSTransactionCompleted, "MTSTxnRM.MTSTransactionCompleted: Currently in " + m_state.ToString() + " state. TxnID = " + ToString());
				}
			}
			catch (Exception ex)
			{
				try
				{
					OracleException.HandleError(OracleTraceLevel.Private, OracleTraceTag.MTS, OracleTraceClassName.CCPMTSTxnRM, OracleTraceFuncName.MTSTransactionCompleted, ex);
				}
				catch
				{
				}
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)4608, OracleTraceClassName.CCPMTSTxnRM, OracleTraceFuncName.MTSTransactionCompleted, ToString());
				}
			}
		}

		internal override void UnRegisteringTxnEvent(Transaction txn)
		{
			try
			{
				txn.TransactionCompleted -= MTSTransactionCompleted;
			}
			catch
			{
			}
		}
	}
}
